//
//  JCTopic.m
//  PSCollectionViewDemo
// 这个思路还是在 需要展示的数据基础上 加两条数据。造一个新的数据数组，这个数组的第一条数据就是需要展示数组的最后一条数据，这个数组的最后一条数据就是需要展示的第一数据
//  Created by jc on 14-1-7.
//
//

#import "JCTopic.h"

@implementation JCTopic
@synthesize JCdelegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        [self setSelf];
    }
    return self;
}
-(void)setSelf{
    self.pagingEnabled = YES;
    self.scrollEnabled = YES;
    self.delegate = self;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.backgroundColor = [UIColor whiteColor];
    _viewArray = [[NSMutableArray alloc]init];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [self setSelf];
    
    // Drawing code
}
-(void)upDate
{
//    [_viewArray removeAllObjects];
    NSMutableArray * tempImageArray = [[NSMutableArray alloc]init];
    
    [tempImageArray addObject:[self.pics lastObject]];//这个思路还是在 需要展示的数据基础上 加两条数据。造一个新的数据数组，这个数组的第一条数据就是需要展示数组的最后一条数据，这个数组的最后一条数据就是需要展示的第一数据
    for (id obj in self.pics) {
        [tempImageArray addObject:obj];
    }
    [tempImageArray addObject:[self.pics objectAtIndex:0]];
    self.pics = Nil;
    self.pics = tempImageArray;
    if (self.style == scrollStyleNone) {
        [self scrollPhotoHandle:0];
        if ([Person sharePerson].scrollTimer) {
            [[Person sharePerson].scrollTimer invalidate];
            [Person sharePerson].scrollTimer = nil;
        }
    }
    else
    {
        [self scrollPhotoHandle:0];
        if ([Person sharePerson].scrollTimer) {
            [[Person sharePerson].scrollTimer invalidate];
            [Person sharePerson].scrollTimer = nil;
        }
        if ([self.pics count]>3) { //表示需要展示的至少有两张图片才会有循环
//            [Person sharePerson].scrollTimer = [NSTimer scheduledTimerWithTimeInterval:self.scrollSecond target:self selector:@selector(scrollTopic) userInfo:nil repeats:YES];
            [Person sharePerson].scrollTimer = [NSTimer timerWithTimeInterval:self.scrollSecond target:self selector:@selector(scrollTopic) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:[Person sharePerson].scrollTimer forMode:NSRunLoopCommonModes];
        }
    }
    
}
-(void)scrollPhotoHandle:(int)index
{
    int i = index;
    for (UIView *sub in self.subviews) {
        [sub removeFromSuperview];
    }
    for (id obj in self.pics) {
//        pic= Nil;
//        pic = [UIButton buttonWithType:UIButtonTypeCustom];
//        pic.imageView.contentMode = UIViewContentModeTop;
//        [pic setFrame:CGRectMake(i*self.frame.size.width,0, self.frame.size.width, self.frame.size.height)];
//        UIImageView * tempImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, pic.frame.size.width, pic.frame.size.height)];
        
        UIImageView *tempImage = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.frame.size.width,0, self.frame.size.width, self.frame.size.height)];
        tempImage.contentMode = UIViewContentModeScaleAspectFill;//UIViewContentModeScaleAspectFill
        [tempImage setClipsToBounds:YES];
       
        NewsRecoImageObj *myobj = (NewsRecoImageObj *)obj;
        NSString *picStr = [NSString isEmpty: myobj.recoImage ]?@"":[NSString stringWithFormat:@"%@_%dx%d.jpg",myobj.recoImage,(int)DeviceWidth,(int)kScaleFrom_iphone6_Desgin(230)];
        CDLog(@"myobj.recoImage=%@",myobj.recoImage);
        //DeviceWidth, kScaleFrom_iphone6_Desgin(230)
        
        if (self.style == scrollStyleNone) {
            if (i==0 || i==1 || i==self.pics.count-1) {
                [tempImage sd_setImageWithURL:[NSURL URLWithString:picStr] placeholderImage:kplaceholderImage240];
            }
        }
        else
        {
            
            if (i==0 || i==1 || i==self.pics.count-1) {
                [tempImage sd_setImageWithURL:[NSURL URLWithString:picStr] placeholderImage:kplaceholderImage240];
            }
        }
        tempImage.tag = 100+i;
        tempImage.userInteractionEnabled =YES;
        UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
        [tempImage addGestureRecognizer:singleTap];
//        [_viewArray addObject:tempImage];
        [self addSubview:tempImage];
        i ++;
    }
    [self setContentSize:CGSizeMake(self.frame.size.width*[self.pics count], self.frame.size.height)];
    [self setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}
- (void)singleTap:(UITapGestureRecognizer *)singtap
{
    [JCdelegate didClick:[self.pics objectAtIndex:(singtap.view.tag-100)] andView:(UIImageView *)singtap.view];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat Width=self.frame.size.width;
    if (scrollView.contentOffset.x == self.frame.size.width) {
        flag = YES;
    }
    if (flag) {
        if (scrollView.contentOffset.x <= 0) {
            [self setContentOffset:CGPointMake(Width*([self.pics count]-2), 0) animated:NO];
        }else if (scrollView.contentOffset.x >= Width*([self.pics count]-1)) {
            [self setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        }
    }
        currentPage = scrollView.contentOffset.x/self.frame.size.width-1;
//    UIImageView *myimage = (UIImageView *)[self viewWithTag:currentPage];
    CDLog(@"currentPage+1=%d  ******* _viewArray.count=%ld self.pics.count=%ld",currentPage+1,_viewArray.count,self.pics.count);
//    UIImageView *imageView = [scrollView viewWithTag:currentPage+1+100];
//    NewsRecoImageObj *myobj = (NewsRecoImageObj *)self.pics[currentPage+1];
//    
//    NSString *picStr = [NSString stringWithFormat:@"%@_%dx%d.jpg",myobj.recoImage ,(int)DeviceWidth,(int)kScaleFrom_iphone6_Desgin(230)];
//    CDLog(@"scrollview 滑动时显示的图片链接为%@",picStr);
//    [imageView sd_setImageWithURL:[NSURL URLWithString:picStr] placeholderImage:kplaceholderImage240];
    UIImageView *preImage;
    UIImageView *myimage;
    UIImageView *nexImage;
    if (currentPage == -1) {
        return;
    }
    else if (currentPage == 0) {//第一页的上一页是需要显示的最后一页
        preImage = [scrollView viewWithTag:self.pics.count-2+100];
        myimage = [scrollView viewWithTag:currentPage+1+100];
        nexImage = [scrollView viewWithTag:currentPage+2+100];
    }
    else if (currentPage == self.pics.count-3)//最后一页的下一页是第一页
    {
        preImage = [scrollView viewWithTag:currentPage+100];
        myimage = [scrollView viewWithTag:currentPage+1+100];
        nexImage = [scrollView viewWithTag:1+100];
    }
    else
    {
        preImage = [scrollView viewWithTag:currentPage+100];
        myimage = [scrollView viewWithTag:currentPage+1+100];
        nexImage = [scrollView viewWithTag:currentPage+2+100];
    }
    NSString *prePicStr;
    NSString *picStr;
    NSString *nexPicStr;
    NewsRecoImageObj *preMyobj ;
    NewsRecoImageObj *myobj ;//= (BuildDetailPhotoObj *)self.pics[currentPage+1];
    NewsRecoImageObj *nexMyobj ;
    
    if (self.pics.count>0) {

        if (currentPage == 0) {//第一页的上一页是需要显示的最后一页
            preMyobj = (NewsRecoImageObj *)self.pics[self.pics.count-2];
           myobj = (NewsRecoImageObj *)self.pics[currentPage+1];
            nexMyobj = (NewsRecoImageObj *)self.pics[currentPage+2];
        }
        else if (currentPage == self.pics.count-3)//最后一页的下一页是第一页
        {
            
            preMyobj = (NewsRecoImageObj *)self.pics[currentPage];
            myobj = (NewsRecoImageObj *)self.pics[currentPage+1];
            nexMyobj = (NewsRecoImageObj *)self.pics[1];
        }
        else
        {
            preMyobj = (NewsRecoImageObj *)self.pics[currentPage];
            myobj = (NewsRecoImageObj *)self.pics[currentPage+1];
            nexMyobj = (NewsRecoImageObj *)self.pics[currentPage+2];
        }
        
        
    }
    else
    {
    
    }
    prePicStr = [NSString stringWithFormat:@"%@_%dx%d.jpg",preMyobj.recoImage ,(int)DeviceWidth,(int)kScaleFrom_iphone6_Desgin(230)];
    picStr = [NSString stringWithFormat:@"%@_%dx%d.jpg",myobj.recoImage ,(int)DeviceWidth,(int)kScaleFrom_iphone6_Desgin(230)];
    nexPicStr = [NSString stringWithFormat:@"%@_%dx%d.jpg",nexMyobj.recoImage ,(int)DeviceWidth,(int)kScaleFrom_iphone6_Desgin(230)];
    [preImage sd_setImageWithURL:[NSURL URLWithString:prePicStr] placeholderImage:kplaceholderImage240];
    [myimage sd_setImageWithURL:[NSURL URLWithString:picStr] placeholderImage:kplaceholderImage240];
    [nexImage sd_setImageWithURL:[NSURL URLWithString:nexPicStr] placeholderImage:kplaceholderImage240];
    CDLog(@"当前currentPage=%d preObj.tag=%d myobj.tag=%d nextObj.tag=%d  prePicStr=%@,###picStr=%@ ###nextPicStr=%@",currentPage,preImage.tag,myimage.tag,nexImage.tag,prePicStr,picStr,nexPicStr);
    
    [JCdelegate currentPage:currentPage total:[self.pics count]-2];
    scrollTopicFlag = currentPage+2==2?2:currentPage+2;
}

-(void)scrollTopic{
    [self setContentOffset:CGPointMake(self.frame.size.width*scrollTopicFlag, 0) animated:YES];
    
    if (scrollTopicFlag > [self.pics count]) {
        scrollTopicFlag = 1;
    }else {
        scrollTopicFlag++;
    }
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (self.style == scrollStyleNone) {
        
    }
    else
    {
//        [Person sharePerson].scrollTimer = [NSTimer scheduledTimerWithTimeInterval:self.scrollSecond target:self selector:@selector(scrollTopic) userInfo:nil repeats:YES];
        [Person sharePerson].scrollTimer = [NSTimer timerWithTimeInterval:self.scrollSecond target:self selector:@selector(scrollTopic) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:[Person sharePerson].scrollTimer forMode:NSRunLoopCommonModes];
        
    }
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    if (self.style != scrollStyleNone) {
        
        if ([Person sharePerson].scrollTimer) {
            [[Person sharePerson].scrollTimer invalidate];
            [Person sharePerson].scrollTimer = nil;
        }
    }
}
-(void)releaseTimer{
    if ([Person sharePerson].scrollTimer) {
        [[Person sharePerson].scrollTimer invalidate];
        [Person sharePerson].scrollTimer = nil;
        
    }
}

@end
